package com.freedom.start.common;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @Author: zhenggang.liu
 * @Date: 2019/8/23 9:03
 */

@Service
public class CommonService {

    @Autowired
    private SessionDAO sessionDAO;

    @Autowired
    private DataSource dataSource;

    public Collection listActiveSession() throws JSONException {
        Collection<Session> activeSessions = sessionDAO.getActiveSessions();
        return activeSessions;
    }

    public void fetchData() {
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT max(sort) from sys_menu");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int sort = resultSet.getInt(1);
                System.out.println("the max is ---: " + sort);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
